package com.rran.study.designpattern.structural.composite;

import java.math.BigDecimal;

/**
 * @author yy
 * @Type Test.java
 * @Desc
 * 模拟买水果时，拿了A,B,C三个袋子，这里的袋子可以看作是一个套餐。
 * 在结账时不用每个袋子去分别计算价格，而是使用组合模式的思想把B袋子和C袋子组合到A袋子里结账，
 * 简化了代码。再出现一个D套餐时也更好扩展
 * @date 2020/7/24 16:54
 */
public class Test {
    public static void main(String[] args) {

        Goods a = new Goods("苹果",new BigDecimal(20));
        Goods aa = new Goods("香蕉",new BigDecimal(20));
        Goods aaa = new Goods("葡萄",new BigDecimal(20));
        GoodsAction packageA = new PackageA();
        packageA.add(a);
        packageA.add(aa);
        packageA.add(aaa);

        Goods b = new Goods("香蕉",new BigDecimal(20));
        Goods bb = new Goods("西瓜",new BigDecimal(20));
        Goods bbb = new Goods("西瓜",new BigDecimal(20));
        GoodsAction packageB = new PackageB();
        packageB.add(b);
        packageB.add(bb);
        packageB.add(bbb);

        Goods c = new Goods("菠萝",new BigDecimal(20));
        Goods cc = new Goods("火龙果",new BigDecimal(20));
        Goods ccc = new Goods("菠萝",new BigDecimal(20));
        GoodsAction packageC = new PackageC();
        packageC.add(c);
        packageC.add(cc);
        packageC.add(ccc);
        //把b袋子和c袋子放到a袋子里一起结账
        packageA.add(packageB);
        packageA.add(packageC);

        BigDecimal price = packageA.acount();
        System.out.println("一共"+price+"元");
    }
}
